//
// Created by baka on 2025/9/29.
//

#ifndef STM32F407IGT6_WEBSERVER_H
#define STM32F407IGT6_WEBSERVER_H



#include <stdbool.h>
#include "httpPacket.h"
#include "sockets.h"


void webserver_init(void);






typedef enum
{
    SERVER_START,
    SERVER_SOCKET,
    SERVER_BIND,
    SERVER_LISTEN,
    SERVER_ACCEPT,
    SERVER_RECV_DATA,
    SERVER_SEND_DATA,
    SERVER_NORMAL_ERROR,
    SERVER_ERROR,
}serverState;

typedef enum
{
    PARSE_WAIT,
    PARSE_SPLIT_BODY,
    PARSE_SPLIT_HEADER,
    PARSE_LINE,
    PARSE_HEADER,
    PARSE_BODY,
    PARSE_GET,
    PARSE_POST,
    PARSE_ERROR,
    PARSE_CMD,
    PARSE_DATA
}parseState;

typedef struct
{

    serverState nextState;
    parseState nextParseState;
    int sfd;
    int cfd;
    uint32_t sendSize;
    uint32_t recvSize;
    int maxRecvSize;
    int maxSendSize;
    struct sockaddr_in server;
    struct sockaddr_in client;
    http_packet_t recvpacket;
    http_packet_t sendpacket;
}serverControl;


void parse_state_transition(serverControl *serverControl);
void server_state_transition(serverControl *serverControl);
bool serverControl_init(serverControl *serverControl);


void user_bind(serverControl *serverControl);
void user_error(serverControl *serverControl);
void user_create_socket(serverControl *serverControl);
void user_read_error(serverControl *serverControl);


void user_split_str_body(serverControl *serverControl);
void user_split_str_header(serverControl *serverControl);
void user_parse_line(serverControl *serverControl);


const char * get_fileType(const char *url);



#endif //STM32F407IGT6_WEBSERVER_H
